<?php

namespace Admin\Controller;

use Think\Controller;


require 'vendor/autoload.php';  //载入
use Qiniu\Auth;   //引入鉴权类
use Qiniu\Storage\UploadManager;     //引入上传类

class UploadController extends CommonController
{
    public function upload()
    {
        $this->display();
    }
    public function uploadApi()
    {
        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = "ve8XV3qPN3BY87pRyBT_Lwc4IbqvRVipVbOkmzhw";
        $secretKey = "2OsGDcmeg6DAVrVRdeNQAsCTfducFKlDVfwdwZ21";
        $bucket = "agecms";

        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);

        // 生成上传 Token
        $token = $auth->uploadToken($bucket);

        // 要上传文件的本地路径,layui默认name为file
        $filePath = $_FILES['file']['tmp_name'];

        // 上传到七牛后保存的文件名
        $project=I('request.project');
        $key = $project.'/'.date('Y-m-d').'/'.time() . '.png';

        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            $list['code'] = 0;
            $list['msg'] = '失败';
            $list['data'] = '';
        } else {
            $host='https://static.agecms.com/';
            $list['code'] = 1;
            $list['msg'] = '成功';
            $list['data'] = $host.$ret['key'];
        }
        $this->ajaxReturn($list);
    }
}
